<?php
namespace app\userapi\controller;
use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $websiteUrl = "http://120.77.150.65";
    /**
     * 轮播图
     */
    public function banner()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $banner = db('yjy_banner')->field("image")->select();
        foreach($banner as $key=>$val){
            $banner[$key]['image'] = $this->websiteUrl.$val['image'];
        }
        $this->success('返回成功',$banner);
    }

    /*
     * 推荐房源
     */
    public function recommend(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $page = $this->request->request("page");
        $list = db('yjy_house')
            ->field("id,image,title,location_near,price")
            ->where('is_recommend',1)
            ->order("createtime desc")
            ->paginate(4);
        $this->success("返回成功",$list);
    }

    //全部
    public function houseAll(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $action = $this->request->request("action");
        //$action = 'near';

        $page = $this->request->request("page");

        if($action == 'all'){
            $list = db("yjy_house")
                ->field("id,image,title,location_near,price,images")
                ->where("house_status",1)
                ->order("createtime desc")
                ->paginate(4);
        }
        if($action == 'near'){ //附近
            $lon = $this->request->request("lon");
            $lat = $this->request->request("lat");
           // $lon = 117.433334350586;
            //$lat = 32.466667175293;
            $list = db("yjy_house")
                ->field("ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lon*PI()/180-lon*PI()/180)/2),2)))*1000) as distance")
                ->field("id,image,images,title,location_near,price")
                ->where("house_status",1)
                ->where("ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lon*PI()/180-lon*PI()/180)/2),2)))*1000) < 1000")
                ->paginate(4);


        }
        $this->success("返回成功",$list);
    }

    //附近房源
    public function houseNear(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $lon = $this->request->request("lon");
        $lat = $this->request->request("lat");

        $list = db("yjy_house a")
            ->field("ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lon*PI()/180-lon*PI()/180)/2),2)))*1000) as distance,a.*")
            ->where("a.house_status",1)
            ->having('distance <= 1000')
            ->paginate(4);





        $sql = "select
    ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lon*PI()/180-lon*PI()/180)/2),2)))*1000) AS distance , a.*
FROM
    fa_yjy_house as a
having
    distance <= 10000 where house_status=1 order by distance asc";
        $result = Db::query($sql);
        $this->success("返回成功",$result);
    }
}
